#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace SpaceGame
{
    /// <summary>
    /// Helper class represents a single entry in a MenuScreen. By default this
    /// just draws the entry text string, but it can be customized to display menu
    /// entries in different ways. This also provides an event that will be raised
    /// when the menu entry is selected.
    /// </summary>
    /// <remarks>Based on a class in the Game State Management sample.</remarks>
    class MenuEntry
    {
        #region Fields

        /// <summary>
        /// The text rendered for this entry.
        /// </summary>
        public string Text;

        /// <summary>
        /// Tracks a fading selection effect on the entry.
        /// </summary>
        /// <remarks>
        /// The entries transition out of the selection effect when they are deselected.
        /// </remarks>
        float selectionFade;

		/// <summary>
		/// The opacity of the button.
		/// </summary>
		public float Alpha = 0f;

		/// <summary>
		/// Invoked when the button is tapped.
		/// </summary>
		public event EventHandler<EventArgs> Tapped;

		/// <summary>
		/// The position of the top-left corner of the button.
		/// </summary>
		public Vector2 Position = Vector2.Zero;

		/// <summary>
		/// The size of the button.
		/// </summary>
		public Vector2 Size = new Vector2(250, 75);

        #endregion

        #region Properties


        #endregion

        #region Events


        /// <summary>
        /// Event raised when the menu entry is selected.
        /// </summary>
        public event EventHandler<EventArgs> Selected;


        /// <summary>
        /// Method for raising the Selected event.
        /// </summary>
        protected internal virtual void OnSelectEntry()
        {
            if (Selected != null)
                Selected(this, EventArgs.Empty);
        }


        #endregion

        #region Initialization


        /// <summary>
        /// Constructs a new menu entry with the specified text.
        /// </summary>
        public MenuEntry(string text)
        {
            this.Text = text;
        }


        #endregion

        #region Update and Draw

		/// <summary>
		/// Invokes the Tapped event and allows subclasses to perform actions when tapped.
		/// </summary>
		protected virtual void OnTapped()
		{
			if (Tapped != null)
				Tapped(this, EventArgs.Empty);
		}

		/// <summary>
		/// Passes a tap location to the button for handling.
		/// </summary>
		/// <param name="tap">The location of the tap.</param>
		/// <returns>True if the button was tapped, false otherwise.</returns>
		public bool HandleTap(Vector2 tap)
		{
			if (tap.X >= Position.X &&
				tap.Y >= Position.Y &&
				tap.X <= Position.X + Size.X &&
				tap.Y <= Position.Y + Size.Y)
			{
				OnTapped();
				return true;
			}

			return false;
		}


        /// <summary>
        /// Updates the menu entry.
        /// </summary>
        public virtual void Update(MenuScreen screen, bool isSelected,
                                                      GameTime gameTime)
        {
            // When the menu selection changes, entries gradually fade between
            // their selected and deselected appearance, rather than instantly
            // popping to the new state.
            float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;

            if (isSelected)
                selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
            else
                selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
        }


        /// <summary>
        /// Draws the menu entry. This can be overridden to customize the appearance.
        /// </summary>
        public virtual void Draw(MenuScreen screen, Vector2 position,
                                 bool isSelected, GameTime gameTime)
        {
			// Compute the button's rectangle
			Rectangle r = new Rectangle(
				(int)Position.X,
				(int)Position.Y,
				(int)Size.X,
				(int)Size.Y);

            // Draw the selected entry in yellow, otherwise white.
            Color color = isSelected ? Color.Yellow : Color.White;
            
            // Pulsate the size of the selected menu entry.
            double time = gameTime.TotalGameTime.TotalSeconds;
            
            float pulsate = (float)Math.Sin(time * 6) + 1;
            
            float scale = 1 + pulsate * 0.05f * selectionFade;

            // Modify the alpha to fade text out during transitions.
            color = new Color(color.R, color.G, color.B, screen.TransitionAlpha);

            // Draw text, centered on the middle of each line.
            ScreenManager screenManager = screen.ScreenManager;
            SpriteBatch spriteBatch = screenManager.SpriteBatch;
            SpriteFont font = screenManager.Font;

            Vector2 origin = new Vector2(0, font.LineSpacing / 2);

			//spriteBatch.DrawString(font, Text, position, color, 0,
			//                       origin, scale, SpriteEffects.None, 0);

			Vector2 textSize = font.MeasureString(Text);
			Vector2 textPosition = new Vector2(r.Center.X, r.Center.Y) - textSize / 2f;
			textPosition.X = (int)textPosition.X;
			textPosition.Y = (int)textPosition.Y;
			spriteBatch.DrawString(font, Text, textPosition, color * Alpha);
        }


        /// <summary>
        /// Queries how much space this menu entry requires.
        /// </summary>
        public virtual int GetHeight(MenuScreen screen)
        {
            return screen.ScreenManager.Font.LineSpacing;
        }


        #endregion
    }
}
